package com.jpo.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {
   static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获取当天时间
    public static Date getDayBegin(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    //获取当天结束时间
    public static Date getDayEnd(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE,59);
        cal.set(Calendar.SECOND,59);
        return cal.getTime();
    }
    //获取当天结束时间
    public static String getDayEndStr(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE,59);
        cal.set(Calendar.SECOND,59);
        return format.format(cal.getTime());
    }
    //获取昨天开始时间
    public static String getBeginDayOfYesterday(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.set(Calendar.DAY_OF_MONTH,-1);
        return format.format(cal.getTime());
    }
    //获取昨天结束时间
    public static String getEndDayOfYesterday(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH,-1);
        return format.format(cal.getTime());
    }
    //获取最近一周开始时间
    public static String  getLastWeek(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DATE,-7);
        return format.format(cal.getTime());

    }
    //获取最近一月开始时间
    public static String  getLastMonth(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.add(Calendar.MONTH,-1);
        return format.format(cal.getTime());
    }
    //获取最近半年开始时间
    public static String  getHalfYear(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.add(Calendar.MONTH,-6);
        return format.format(cal.getTime());
    }
    //获取最近一年开始时间
    public static String  getLastYear(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.add(Calendar.YEAR,-1);
        return format.format(cal.getTime());
    }
    //获取今年迄今开始时间
    public static String  getBeginOfYear(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR,cal.get(Calendar.YEAR));
        cal.set(Calendar.MONTH,Calendar.JANUARY);
        cal.set(Calendar.DATE,1);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        return format.format(cal.getTime());
    }
    //获取上一年度开始时间
    public static String  getBeginOfYesterYear(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR,cal.get(Calendar.YEAR));
        cal.set(Calendar.MONTH,Calendar.JANUARY);
        cal.set(Calendar.DATE,1);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        cal.add(Calendar.YEAR,-1);
        return format.format(cal.getTime());
    }
    //获取上一年度结束时间
    public static String  getEndOfYesterYear(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.add(Calendar.YEAR,-1);
        return format.format(cal.getTime());
    }
}
